﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutList.cshtml";
}

<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="State" value="{{d.Id}}" lay-skin="switch" lay-text="启动|停止" lay-filter="stateDemo" {{ d.State == 1 ? 'checked' : '' }}>
</script>

<script>
    layui.use(["element", "table","layer","jquery","form", "okLayer", "okUtils"], function () {
        let table = layui.table;
        let form = layui.form;
        let layer = layui.layer;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let jquery = layer.jquery;

        let AllTable = table.render({
            elem: "#tableId",
            url: "/Security/Quartz/List",
            limit: 10,
            page: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                { type: "checkbox" },
                { field: "Id", title: "ID", width: 80, sort: true },
                { field: "FileName", title: "IJob实现", width: 250 },
                { field: "JobGroup", title: "任务组", width: 250 },
                { field: "JobName", title: "任务名称", width: 200 },
                { field: "CronExpress", title: "Cron表达式", width: 150 },
                { field: "StarRunTime", title: "启动时间", width: 150 },
                { field: "EndRunTime", title: "停止时间", width: 150 },
                { field: "State", title: "状态", width: 100, templet: '#switchTpl', unresize: true},
                { field: "CreateTime", title: "创建时间", width: 150, templet: '<span>{{showDate(d.CreateTime)}}<span>' },
                { title: "操作", width: 100,   hight:24, align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        form.on("submit(search)", function (data) {
            AllTable.reload({
                where: data.field,
                page: { curr: 1 }
            });
            return false;
        });
        //监听任务启动停止切换按钮
        form.on('switch(stateDemo)', function (data) {
            var id = this.value;
            var isState = data.elem.checked;
            okUtils.ajax("/Security/Quartz/ScheduleJob?id=" + id + "&isState=" + isState, "get", null, true).done(function (response) {
                okLayer.greenTickMsg(response.message, function () {
                    location.reload(); // 页面刷新
                });
            }).fail(function (error) {
                console.log(error)
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "batchDel":
                    batchDel();
                    break;
                case "add":
                    add();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "del":
                    del(data.Id);//field Id 和 数据库表字段 Id 要一致
                    break;
            }
        });

        function add() {
            okLayer.open("添加任务", "/Security/Quartz/Add", "50%", "100%", null, null);
        }

        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                let idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/Security/Quartz/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                        okUtils.tableSuccessMsg(response.message);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/Security/Quartz/Delete", "get", { id: id }, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }

    })
</script>


<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    @Html.TopToolBarHtml(ViewData["TopButtonList"])
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    @Html.RightToolBarHtml(ViewData["RightButtonList"])
</script>